package com.zyh.controller;

import com.zyh.entity.Book;
import com.zyh.entity.Category;
import com.zyh.service.BookService;
import com.zyh.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private ModelAndView mav;
    @Autowired
    private BookService bookService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("selectAll")
    public ModelAndView selectAll(){
        List<Book> inventoryDescBookList = bookService.selectOrderByInventoryDesc();
        List<Book> saleNumDescBookList = bookService.selectOrderBySaleNumDesc();
        List<Book> groundingDateDescBookList = bookService.selectOrderByGroundingDateDesc();
        List<Book> dateAndSaleDescBookList = bookService.selectOrderByDateAndSaleDesc();
        mav.addObject("inventoryDescBookList",inventoryDescBookList);
        mav.addObject("saleNumDescBookList",saleNumDescBookList);
        mav.addObject("groundingDateDescBookList",groundingDateDescBookList);
        mav.addObject("dateAndSaleDescBookList",dateAndSaleDescBookList);

        List<Category> categories = categoryService.selectAll();
        mav.addObject("categories",categories);

        mav.setViewName("/main/main.jsp");
        return mav;
    }
    @RequestMapping("selectByBookId")
    public ModelAndView selectByBookId(Integer bookId){
        Book book = bookService.selectByBookId(bookId);
        mav.addObject("book",book);

        mav.setViewName("/main/product.jsp");
        return mav;
    }

    @RequestMapping("selectByLevelCid")
    public ModelAndView selectByLevelCid(Integer cId){
        List<Book> bookList = bookService.selectByLevelCid(cId);
        List<Category> categories = categoryService.selectCateByLevelTwo(cId);

        int count = 0;
        for (Category c : categories
        ) {
            count += c.getCateNum();
        }
        mav.addObject("books",bookList);
        mav.addObject("cates",categories);
        mav.addObject("count",count);
        mav.setViewName("/main/book_list.jsp");
        return mav;
    }

    @RequestMapping("selectByLevelParentId")
    public ModelAndView selectByLevelParentId(Integer cId){
        List<Book> bookList = bookService.selectByLevelParentId(cId);
        List<Category> categories = categoryService.selectCateByLevelOne(cId);

        int count = 0;
        for (Category c : categories
             ) {
            count += c.getCateNum();
        }
        mav.addObject("books",bookList);
        mav.addObject("cates",categories);
        mav.addObject("count",count);
        mav.setViewName("/main/book_list.jsp");
        return mav;
    }

}
